const {Password} = require('../../model/password')
const {User} = require('../../model/user')
const hasher = require('pbkdf2-password')()

module.exports = async (req,res) => {
        const {username,email,password,role,state} = req.body

        if(username.trim().length == 0 || email.trim().length == 0|| password.trim().length == 0 ){
           return res.render('admin/user-edit',{
                username:req.session.username,
                message:'参数错误：参数不能为空'
            })
        }
        let resObj = await User.findOne({email})
        if(resObj) {
          return  res.render('admin/user-edit',{
                username:req.session.username,
                message:'该邮箱已注册'
            })
        }
         hasher({password}, async (err, pass, salt, hash)=>{
            // if(err) throw err
            let doc = await Password.create({salt,hash})
            let result = await User.create({username,email,password:doc._id,state,role})
            res.redirect('/admin/user')
        })

}

